package com.zenika.zentracker.hibernate.info;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * This class is used to collect information in the case Hibernate Removes a Collection.
 * 
 * @see CollectionInformation
 * @see AbstractCollectionInformation
 * @author Zenika
 */
public class CollectionRemove extends AbstractCollectionInformation implements CollectionInformation {

    private List previousObjects = new ArrayList();

    /**
     * The flush index and the old collection are needed to create a CollectionRemove Information.
     * 
     * @param flushNumber the flush index before a transaction completion
     * @param oldValues the objects previously in the collection
     */
    public CollectionRemove(final int flushNumber, final Serializable oldValues) {
        super(flushNumber);
        if (oldValues instanceof Map) {
            this.previousObjects.addAll(((Map) oldValues).values());
        } else {
            this.previousObjects.addAll(((List) oldValues));
        }
    }

    public String toString() {
        return logRemovingElementsFromCollection(previousObjects);
    }

}
